package com.lanlian.cayman.utils;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.NumberFormat;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author yuguohe
 * <p>
 * email:1374405188@qq.com
 * </p>
 * 2015-1-6
 */
public class NumberManager {
    /**
     * 四色五入
     * 将2个浮点数进行乘法运算 默认保留2位小数
     *
     * @param number  double的字符串表示
     * @param number2 double的字符串表示
     * @param scale   保留几位小数
     * @return 想乘后的结果
     */
    public static String getString2(String number, String number2, int scale) {

        BigDecimal bd4 = null;
        try {

            if (number == null || "".equals(number) || "null".equals(number)) {
                number = "0";
            }
            if (number2 == null || "".equals(number2) || "null".equals(number2)) {
                number2 = "0";
            }
            BigDecimal bd = new BigDecimal(number);
            BigDecimal bd2 = new BigDecimal(number2);
            BigDecimal bd3 = bd.multiply(bd2);
            bd4 = bd3.setScale(scale, BigDecimal.ROUND_HALF_UP);
        } catch (Exception e) {
            e.printStackTrace();
            return "0.00";
        }
        return bd4.toString();
    }

    public static int getWei(String s1) {
        int i = 0;
        int index = s1.indexOf(".");
        if (index < 0) {
            return 0;
        }
        int length = s1.length();
        i = length - index - 1;

        return i;
    }

    /**
     * 将2个浮点数进行乘法运算 保留最佳位数
     *
     * @param number  double的字符串表示
     * @param number2 double的字符串表示
     * @param scale   保留几位小数
     * @return 想乘后的结果
     */
    public static String getStringBy(String number, String number2, int scale) {
        int a0 = 0;
        int a1 = getWei(number);
        int a2 = getWei(number2);
        a0 = a1;
        if (a1 < a2) {
            a0 = a2;
            if (a0 > scale) {
                a0 = scale;
            }
        }


        BigDecimal bd4 = null;
        try {

            if (number == null || "".equals(number) || "null".equals(number)) {
                number = "0";
            }
            if (number2 == null || "".equals(number2) || "null".equals(number2)) {
                number2 = "0";
            }
            BigDecimal bd = new BigDecimal(number);
            BigDecimal bd2 = new BigDecimal(number2);
            BigDecimal bd3 = bd.multiply(bd2);

            bd4 = bd3.setScale(a0, BigDecimal.ROUND_DOWN);
            String s0 = bd4.toString();
//			System.out.println(s0);
        } catch (Exception e) {
            e.printStackTrace();
            return "0.00";
        }
        return bd4.toPlainString();
    }

    /**
     * 将2个浮点数进行乘法运算 默认保留2位小数
     *
     * @param number  double的字符串表示
     * @param number2 double的字符串表示
     * @param scale   保留几位小数
     *                去除结尾后面的0
     * @return 想乘后的结果
     */
    public static String getStringNoZero(String number, String number2, int scale) {

        BigDecimal bd4 = null;
        String s;
        try {

            if (number == null || "".equals(number) || "null".equals(number)) {
                number = "0";
            }
            if (number2 == null || "".equals(number2) || "null".equals(number2)) {
                number2 = "0";
            }
            BigDecimal bd = new BigDecimal(number);
            BigDecimal bd2 = new BigDecimal(number2);
            BigDecimal bd3 = bd.multiply(bd2);

            bd4 = bd3.setScale(scale,BigDecimal.ROUND_HALF_UP);
            s = bd4.toPlainString();
            if (s.indexOf(".") > 0) {

                //正则表达

                s = s.replaceAll("0+?$", "");//去掉后面无用的零

                s = s.replaceAll("[.]$", "");//如小数点后面全是零则去掉小数点

            }
//		System.out.println(s0);
        } catch (Exception e) {
            e.printStackTrace();
            return "0.00";
        }
        return s;
    }
    /**
     * 将2个浮点数进行乘法运算 默认保留2位小数
     *不4舍五入
     * @param number  double的字符串表示
     * @param number2 double的字符串表示
     * @param scale   保留几位小数
     * @return 想乘后的结果
     */
    public static String getStringNot4or5(String number, String number2, int scale) {

        BigDecimal bd4 = null;
        try {

            if (number == null || "".equals(number) || "null".equals(number)) {
                number = "0";
            }
            if (number2 == null || "".equals(number2) || "null".equals(number2)) {
                number2 = "0";
            }
            BigDecimal bd = new BigDecimal(number);
            BigDecimal bd2 = new BigDecimal(number2);
            BigDecimal bd3 = bd.multiply(bd2);

            bd4 = bd3.setScale(scale, BigDecimal.ROUND_DOWN);
            String s0 = bd4.toString();
//		System.out.println(s0);
        } catch (Exception e) {
            e.printStackTrace();
            return "0.00";
        }
        return bd4.toPlainString();
    }
    /**
     * 将2个浮点数进行乘法运算 默认保留2位小数
     *4舍五入
     * @param number  double的字符串表示
     * @param number2 double的字符串表示
     * @param scale   保留几位小数
     * @return 想乘后的结果
     */
    public static String getString4or5(String number, String number2, int scale) {

        BigDecimal bd4 = null;
        try {

            if (number == null || "".equals(number) || "null".equals(number)) {
                number = "0";
            }
            if (number2 == null || "".equals(number2) || "null".equals(number2)) {
                number2 = "0";
            }
            BigDecimal bd = new BigDecimal(number);
            BigDecimal bd2 = new BigDecimal(number2);
            BigDecimal bd3 = bd.multiply(bd2);

            bd4 = bd3.setScale(scale, BigDecimal.ROUND_HALF_UP);
            String s0 = bd4.toString();
//		System.out.println(s0);
        } catch (Exception e) {
            e.printStackTrace();
            return "0.00";
        }
        return bd4.toPlainString();
    }
    /**
     * 将2个浮点数进行乘法运算 默认保留2位小数
     *
     * @param number  double的字符串表示
     * @param number2 double的字符串表示
     * @param scale   保留几位小数
     * @return 想乘后的结果
     */
    public static String getString(String number, String number2, int scale) {

        BigDecimal bd4 = null;
        try {

            if (number == null || "".equals(number) || "null".equals(number)) {
                number = "0";
            }
            if (number2 == null || "".equals(number2) || "null".equals(number2)) {
                number2 = "0";
            }
            BigDecimal bd = new BigDecimal(number);
            BigDecimal bd2 = new BigDecimal(number2);
            BigDecimal bd3 = bd.multiply(bd2);

            bd4 = bd3.setScale(scale, BigDecimal.ROUND_HALF_UP);
            String s0 = bd4.toString();
//		System.out.println(s0);
        } catch (Exception e) {
            e.printStackTrace();
            return "0.00";
        }
        return bd4.toPlainString();
    }

    public static String getString(String number, String number2, int scale, int type) {

        BigDecimal bd4 = null;
        try {

            if (number == null || "".equals(number) || "null".equals(number)) {
                number = "0";
            }
            if (number2 == null || "".equals(number2) || "null".equals(number2)) {
                number2 = "0";
            }
            BigDecimal bd = new BigDecimal(number);
            BigDecimal bd2 = new BigDecimal(number2);
            BigDecimal bd3 = bd.multiply(bd2);

            bd4 = bd3.setScale(scale, type);
            String s0 = bd4.toString();
//		System.out.println(s0);
        } catch (Exception e) {
            e.printStackTrace();
            return "0.00";
        }
        return bd4.toPlainString();
    }

    /**
     * 进行浮点除法运算
     *
     * @param number  double的字符串表示
     * @param number2 double的字符串表示
     * @param scale   保留几位小数
     * @return
     */
    public static String getDivString(String number, String number2, int scale) {
        BigDecimal bd4 = null;
        try {
            if (number == null || "".equals(number) || "null".equals(number)) {
                number = "0";
            }
            if (number2 == null || "".equals(number2) || "null".equals(number2)) {
                number2 = "0";
                return "0.00";
            }
            BigDecimal bd = new BigDecimal(number);
            BigDecimal bd2 = new BigDecimal(number2);
            BigDecimal bd3 = bd.divide(bd2, scale, BigDecimal.ROUND_DOWN);
            bd4 = bd3.setScale(scale, BigDecimal.ROUND_DOWN);
        } catch (Exception e) {
            e.printStackTrace();
            return "0.00";
        }
        return bd4.toPlainString();
    }

    /**
     * 减法
     *
     * @param number
     * @param number2
     * @param scale
     * @return
     */
    public static String getSubString(String number, String number2, int scale) {
        BigDecimal bd4 = null;
        try {
            if (number == null || "".equals(number) || "null".equals(number)) {
                number = "0";
            }
            if (number2 == null || "".equals(number2) || "null".equals(number2)) {
                number2 = "0";
            }
            int a1 = getWei(number);
            if (scale < 0) {
                scale = a1;
            }
            BigDecimal bd = new BigDecimal(number);
            BigDecimal bd2 = new BigDecimal(number2);
            BigDecimal bd3 = bd.subtract(bd2);
            bd4 = bd3.setScale(scale, BigDecimal.ROUND_DOWN);
        } catch (Exception e) {
            e.printStackTrace();
            return "0.00";
        }
        return bd4.toPlainString();
    }

    /**
     * 加法
     *
     * @param number
     * @param number2
     * @param scale
     * @return
     */
    public static String getAddString(String number, String number2, int scale) {
        BigDecimal bd4 = null;
        try {


            if (number == null || "".equals(number) || "null".equals(number)) {
                number = "0";
            }
            if (number2 == null || "".equals(number2) || "null".equals(number2)) {
                number2 = "0";
            }
            int a1 = getWei(number);
            if (scale < 0) {
                scale = a1;
            }
            BigDecimal bd = new BigDecimal(number);
            BigDecimal bd2 = new BigDecimal(number2);
            BigDecimal bd3 = bd.add(bd2);
            bd4 = bd3.setScale(scale, BigDecimal.ROUND_DOWN);
        } catch (Exception e) {
            e.printStackTrace();
            return "0.00";
        }
        return bd4.toPlainString();
    }

    /**
     * 判断一个字符串是否全为数字
     *
     * @param str
     * @return
     */
    public static boolean isNumeric(String str) {
        Pattern pattern = Pattern.compile("[0-9]*");
        Matcher isNum = pattern.matcher(str);
        if (!isNum.matches()) {
            return false;
        }
        return true;
    }
    //判断浮点数（double和float）
    public static boolean isDouble(String str) {
        if (null == str || "".equals(str)) {
            return false;
        }
        Pattern pattern = Pattern.compile("^[-\\+]?[.\\d]*$");
        return pattern.matcher(str).matches();
    }


    /**
     * @param str
     * @param str2
     * @return 1 str 大于 str2    0等于  -1 小于
     */
    public static int isDaYu(String str, String str2) {
        int i = 0;
        try {
            double d1 = Double.parseDouble(str);
            double d2 = Double.parseDouble(str2);
            if (d1 > d2) {
                i = 1;
            } else if (d1 == d2) {

                i = 0;
            } else {
                i = -1;
            }
        } catch (Exception e) {
            e.printStackTrace();

        }
        return i;
    }


    /**
     * 得到格式化后的
     *
     * @param str
     * @param scale 保留的位数
     * @return
     */
    public static String getGeshiHua(String str, int scale) {
        String s0 = "0.00";
        String s = NumberManager.getString(str, "1", scale);
        try {

            NumberFormat usFormat0 = NumberFormat.getNumberInstance();
            usFormat0.setRoundingMode(RoundingMode.DOWN);
            usFormat0.setMinimumFractionDigits(scale);
            usFormat0.setMaximumFractionDigits(scale);
            s0 = usFormat0.format(Double.parseDouble(str));
        } catch (Exception e) {
            e.printStackTrace();
            return "0.00";
        }
        return s0;
    }

    /**
     * 将字符串转化为double 失败返回0
     *
     * @param str
     * @return
     */
    public static double StoD(String str) {
        double d = 0;
        try {
            d = Double.parseDouble(str);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return d;
    }

    /**
     * 返回金额小数位数
     *
     * @param balance
     * @return
     */
    public static int getNumberDecimalDigits(String balance) {
        int dcimalDigits = 0;
        if (balance.contains("E") || balance.contains("e")) {
            int num;
            try {
                num = Integer.valueOf(balance.substring(balance.length() - 1, balance.length()));
            } catch (Exception e) {
                num = 4;
                e.printStackTrace();
            }
            return num;
        }
        BigDecimal bigDecimal = new BigDecimal(balance);
        NumberFormat nf = NumberFormat.getInstance();
        String balanceStr = nf.format(bigDecimal.doubleValue());
        int indexOf = balanceStr.indexOf(".");
        if (indexOf > 0) {
            dcimalDigits = balanceStr.length() - 1 - indexOf;
        }
        return dcimalDigits;
    }

}
